###################################################################################################
## Payson (2020) "The Partisan Logic of City Mobilization" Replication Code
## California Analysis (Table 3)
## Created with R version 3.4.1 (2017-06-30) -- "Single Candle"
###################################################################################################

## Set working directory
setwd()


## Load packages
library(dplyr); library(lfe)


## Functions for clean tex tables (Note: tables will not compile without these functions)
pretty <- function(x) {
  paste(prettyNum(x, big.mark = ","))}

num.cities <- function(x) {
  length(which(getfe(x)=="fips"))}

N <- function(x) {
  x$N}

getmean <- function(x) {
  round(mean(x$response), 2)}


###################################################################################################

## load ca.csv
dta <- read.csv("ca.csv")



###################################################################################################
## Table 3: Fixed vs. Time-Varying City Partisanship: CA Robustness Check
###################################################################################################


## Replicate effect of partisan mismatch on lobbying using fixed tercile in 2008
m <- felm(lobby ~ fixed.mismatch + log.pop | fips + year | 0 | fips + year, 
          data = subset(dta, dem.tercile != "Moderate"))

## Time-varying measure of partisanship
m1 <- felm(lobby ~ mismatch + log.pop | fips + year | 0 | fips + year, 
           data = subset(dta, dem.tercile != "Moderate"))


stargazer(m, m1, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          title = c("Fixed vs. Time-Varying City Partisanship: CA Robustness Check."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          keep = c("mismatch.fixed", "mismatch"),
          covariate.labels = c("Mismatch (Fixed City Partisanship)",
                               "Mismatch (Time-Varying City Partisanship)"),
          add.lines = list(c("City FEs", "Y", "Y"),
                           c("Year FEs", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Models control for population. Robust standard errors clustered by city.")

